package com.ohs.ohsoa.system.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.ohs.ohsoa.system.dto.DepartmentDto;
import com.ohs.ohsoa.system.entity.Department;
import com.ohs.ohsoa.system.service.DepartmentService;
import com.ohs.ohsoa.system.vo.DepartmentVo;
import com.ohs.ohsoa.utils.auth.UserLoginToken;
import com.ohs.ohsoa.utils.bean.requestBean.PageParam;
import com.ohs.ohsoa.utils.bean.responseBean.ResultBean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * 部门Controller
 *
 * @author qianjp
 */

@Api(value = "部门管理", tags = "部门管理")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    /**
     * 获取部门列表信息
     *
     * @param pageParam 検索用パラメータ
     * @return 部門情報
     */
    @UserLoginToken
    @PostMapping(value = "/select/list")
    @ApiOperation(httpMethod = "POST", value = "获取部门列表信息")
    public ResultBean<PageInfo<DepartmentVo>> departmentSelectList(@RequestBody PageParam<DepartmentDto> pageParam) {
        List<DepartmentVo> res = departmentService.departmentSelectList(pageParam);
        return ResultBean.success(new PageInfo<>(res));
    }



    /**
     * 根据id获取部门详细信息
     *
     * @param departmentDto 部门数据
     * @return departGuid必传
     */
    @UserLoginToken
    @PostMapping(value = "/select")
    @ApiOperation(httpMethod = "POST", value = "根据id获取部门详细信息")
    public ResultBean<DepartmentVo> departmentSelect(@RequestBody DepartmentDto departmentDto) {
        return ResultBean.success(departmentService.departmentSelect(departmentDto));
    }


    /**
     * 新增部门
     *
     * @param departmentDto 部门数据
     * @return 更新结果
     */
    @UserLoginToken
    @PostMapping(value = "/insert")
    @ApiOperation(httpMethod = "POST", value = "新增部门")
    public ResultBean<Boolean> departmentInsert(@RequestBody @Valid DepartmentDto departmentDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBean.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultBean.success(departmentService.departmentInsert(departmentDto));
    }


    /**
     * 删除部门
     *
     * @param departmentDto 部门数据
     * @return 删除结果
     */
    @UserLoginToken
    @DeleteMapping(value = "/delete")
    @ApiOperation(httpMethod = "DELETE", value = "删除部门")
    public ResultBean<Boolean> departmentDelete(@RequestBody DepartmentDto departmentDto) {
        return ResultBean.success(departmentService.departmentDelete(departmentDto));
    }


    /**
     * 修改部门信息
     *
     * @param departmentDto 部门数据
     * @return 修改结果
     */
    @UserLoginToken
    @PutMapping(value = "/update")
    @ApiOperation(httpMethod = "PUT", value = "修改部门信息")
    public ResultBean<Boolean> departmentUpdate(@RequestBody @Valid DepartmentDto departmentDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBean.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultBean.success(departmentService.departmentUpdate(departmentDto));
    }


    /**
     * 通过企业查询部门下拉列表
     *
     * @param departmentDto 部门数据
     * @return 部门列表
     */
    @UserLoginToken
    @PostMapping(value = "/selectList/byEnterprise")
    @ApiOperation(httpMethod = "POST", value = "通过企业查询部门下拉列表")
    public ResultBean<List<Department>> departmentSelectListByEnterprise(@RequestBody DepartmentDto departmentDto) {
        return ResultBean.success(departmentService.departmentSelectListByEnterprise(departmentDto));
    }


}
